package org.etom.impl.ui.actions;

import org.eclipse.jface.action.Action;
import org.eclipse.jface.dialogs.MessageDialog;
import org.eclipse.jface.resource.ImageDescriptor;
import org.eclipse.osgi.util.NLS;
import org.eclipse.swt.widgets.Shell;
import org.etom.connector.datatypes.IMoodleConnection;
import org.etom.impl.ui.Activator;
import org.etom.impl.ui.common.IconConstants;
import org.etom.impl.ui.common.ResourcesTreeViewer;
import org.etom.impl.ui.util.Utility;
import org.etom.impl.ui.views.ViewsMessages;

/**
 * An action class which is responsible for the delete
 * of the available connection.
 */
public class DeleteAccountAction extends Action {
  
  private Shell shell;
  private ResourcesTreeViewer resourcesTreeViewer;
  
  /**
   * Construct a new delete action.
   * 
   * @param shell - the shell which is used for the necessary dialogs
   * @param provider - the resource provider
   */
  public DeleteAccountAction(Shell shell, ResourcesTreeViewer resourcesTreeViewer) {
    this.shell = shell;
    this.resourcesTreeViewer = resourcesTreeViewer;
    init();
  }
  
  /**
   * @see org.eclipse.jface.action.Action#run()
   */
  @Override
  public void run() {
    IMoodleConnection[] selectedConections = resourcesTreeViewer.getSelectedConnections();
    if (selectedConections.length == 0) return; //do nothing here
    String message;
    if (selectedConections.length == 1) {
      message = NLS.bind(ViewsMessages.Resources_Delete_Confirmation_Single,
          Utility.toString(selectedConections[0]));
    } else {
      message = NLS.bind(ViewsMessages.Resources_Delete_Confirmation_Multi,
          Integer.toString(selectedConections.length));
    }
    if (MessageDialog.openQuestion(
        shell, ViewsMessages.Resources_Delete_Confirmation_Title, message)) {
      resourcesTreeViewer.removeMoodleConnections(selectedConections);
    }
  }

  /*
   * An utility method which initializes the the delete action.
   */
  private void init() {
    setText(ViewsMessages.Resources_Context_Menu_Delete);
    setImageDescriptor(ImageDescriptor.createFromFile(Activator.class,
        IconConstants.MENU_DELETE_ICON_PATH));
  }
  
}
